package com.webserve.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class HttpServletRequest {
    private Socket socket;
    private String method;//请求方式
    private String uri;//抽象路径
    private String protocol;//协议版本
    private Map<String, String> header = new HashMap<>();//使用散列表储存消息头

    private String requestURI;//请求路径
    private String queryString;//具体的请求
    private Map<String, String> parameters = new HashMap<>();//将具体的请求存储在散列表中


    public HttpServletRequest(Socket socket) throws IOException, EmptyRequestException {
        this.socket = socket;
        parseRequestLine();
        parseHeaders();
        parseContent();
    }


    /**
     * 解析请求行
     */
    public void parseRequestLine() throws IOException, EmptyRequestException {
        String line = readline();
//        line = URLDecoder.decode(line, "utf-8");
        if (line.isEmpty()) {
            throw new EmptyRequestException();
        }
        String[] strings = line.split("\\s");
        method = strings[0];
        uri = strings[1];
        parseURI();
        protocol = strings[2];
    }

    /**
     * 解析消息头
     */
    public void parseHeaders() throws IOException {
        while (true) {
            String line = readline();
            if (line.equals("")) {
                return;
            }
            String[] str = line.split(":\\s", 2);
            header.put(str[0], str[1]);

        }
    }

    /**
     * 解析消息行
     */
    public void parseContent() {
    }


    /**
     * 设置readline方法读一行客户端发出的请求,实现了代码的服用
     */
    public String readline() throws IOException {
        InputStream is = socket.getInputStream();
        StringBuilder str = new StringBuilder();
        char pre = 0, cur = 0;//pre是上次读到的内容,cur是本次读到的内容.循环结束前,将cur赋值给pre;
        int c;
        while ((c = is.read()) != -1) {
            cur = (char) c;
            if (pre == 13 && cur == 10) { //如果pre为回车且cur为换行,则终止循环
                break;
            }

            str.append(cur); //将这次的char数值添加到str后
            pre = cur;  //将cur赋值给pre
        }
        return str.toString().trim();
    }

    public void parseURI() {
        String[] s = uri.split("\\?", 2);
        if (s.length != 1) {
            requestURI = s[0];
            queryString = s[1];
            try {
                queryString = URLDecoder.decode(queryString,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            s = queryString.split("&");
            for (String ss : s) {
                String[] a = ss.split("=", 2);
                parameters.put(a[0], a[1]);
            }
        } else {
            requestURI = uri;
        }

    }

    public String getRequestURI() {
        return requestURI;
    }

    public String getQueryString() {
        return queryString;
    }

    public Map<String, String> getParameters() {
        return parameters;
    }

    public String getMethod() {
        return method;
    }

    public String getUri() {
        return uri;
    }

    public String getProtocol() {
        return protocol;
    }

    public Map<String, String> getHeader() {
        return header;
    }
}
